package com.jiaai.cloud.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.base.util.IdWorkUtil;
import com.jiaai.cloud.base.util.RestResultGenerator;
import com.jiaai.cloud.user.entity.*;
import com.jiaai.cloud.user.service.*;
import com.jiaai.cloud.utils.HttpHeaderUtils;
import org.bouncycastle.crypto.tls.TlsAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author 佳爱网络科技
 * @version V1.0
 * @title UserAPI
 * @since 2017/8/1
 */
@RequestMapping("/api/v1")
@RestController
public class UserAPI {

    @Autowired
    private ITUserWalletService userWalletService;

    @Autowired
    private  ITUserService userService;


    @Autowired
    private IdWorkUtil idWorkUtil;

    @Autowired
    private  ITUserLevelSettingService levelSettingService;

    @Autowired
    private ITUserLevelService userLevelService;

    @Autowired
    private ITUserIdentityService userIdentityService;


    @PostMapping("/live/login")
    public  Object login(@RequestBody  TUser user)throws ServiceException{
        TUser oldUser =userService.selectById(user.getUserId());
        if(oldUser==null){
            oldUser=user;
            oldUser.setUserNo(idWorkUtil.generateId("userNo",4));
            oldUser.setCreateTime(new Date());
            TUserWallet wallet=new TUserWallet();
            wallet.setMoney(BigDecimal.valueOf(0));
            wallet.setUserId(user.getUserId());
            userWalletService.insert(wallet);
            TUserLevelSetting setting=levelSettingService.selectOne(new EntityWrapper().eq("lv",1));
            TUserLevel userLevel=new TUserLevel();
            userLevel.setCurrentExp(Long.valueOf(0));
            userLevel.setNexLvExp(Long.valueOf(setting.getExp()));
            userLevel.setLv(1);
            userLevel.setUserId(user.getUserId());
            userLevelService.insert(userLevel);
        }else {
            oldUser.setNickName(user.getNickName());
            oldUser.setAvatar(user.getAvatar());
            oldUser.setSex(user.getSex());
            oldUser.setCity(user.getCity());


        }
        userService.insertOrUpdate(oldUser);
        return RestResultGenerator.genSuccessResult(userService.findUserVOById(user.getUserId()));
    }

    /**
     * 用户实名认证
     * @param userIdentity
     * @return
     * @throws ServiceException
     */
    @PostMapping("/authentication")
    public  Object  authentication(@RequestBody TUserIdentity userIdentity)throws ServiceException{
        try {
            userIdentity.setUserId(HttpHeaderUtils.getUserId());
        }catch (Exception e){
            throw  new ServiceException("获取用户信息失败!",e.getMessage());
        }
        userIdentityService.insert(userIdentity);
        return RestResultGenerator.genSuccessResult();
    }
}
